#include <glib.h>

static void _ptr_array_foreach_print_func(gpointer data, gpointer user_data)
{
    g_print("%s: %s(addr:%p) \n", (gchar *)user_data, (gchar *)data, data);
}

static void _ptr_array_free_func(gpointer data)
{
    if(NULL != data) {
        g_free(data);
    }
}

static void _ptr_array_add(GPtrArray *parr)
{
    gchar str1[8] = {0};
    gchar str2[8] = {0};
    gchar str3[8] = {0};

    g_strlcpy(str1, "one", 8);
    g_strlcpy(str2, "two", 8);
    g_strlcpy(str3, "three", 8);

    g_ptr_array_add(parr, (gpointer)g_strdup(str1));
    g_ptr_array_add(parr, (gpointer)g_strdup(str3));
    g_ptr_array_insert(parr, 1, (gpointer)g_strdup(str2));

    g_ptr_array_foreach(parr, _ptr_array_foreach_print_func, "in-func");
}

gint main(gint argc, gchar **argv)
{
    GPtrArray *parr = NULL;
    gint i = 0;

    parr = g_ptr_array_sized_new(10);

    _ptr_array_add(parr);

    for(i=0; i<parr->len; i++) {
        g_print("parr[%d](addr:%p):%s(addr:%p) \n", 
            i, 
            &g_ptr_array_index(parr, i), 
            (gchar *)g_ptr_array_index(parr, i), 
            g_ptr_array_index(parr, i));
    }

    g_ptr_array_foreach(parr, _ptr_array_foreach_print_func, "out-func");

    g_ptr_array_set_free_func(parr, _ptr_array_free_func);
    g_ptr_array_free(parr, TRUE);

    return 0;
}